package mapping

import (
	"CSMS/DAO"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

//LoginPOST 用户登录操作
func LoginPOST(c *gin.Context) {
	/**
		获取用户传递表单，并检测相关信息是否正确
	**/
	UserLoginId := c.PostForm("UserLoginId")
	password := c.PostForm("password")
	//获取登录用户的相关信息
	user1 := DAO.User{}
	if !user1.SelectUserByLoginId(UserLoginId) {
		c.JSON(http.StatusOK, gin.H{
			"code":     "loginFailed",
			"userRank": "none",
		})
		return
	}
	fmt.Println(user1.UserPassword.String)
	//不正确，返回
	if password != user1.UserPassword.String {
		c.JSON(http.StatusOK, gin.H{
			"code":     "loginFailed",
			"userRank": "none",
		})
		return
	}
	/**
		判断用户类型以及返回对应的Cookie以及json
	**/
	userRank := user1.UserRank.String
	//给其设置一个临时的用户号cookie以及用户职称cookie，
	//用户职称Cookie
	cookie := &http.Cookie{
		Name:  "userRank",
		Value: userRank,
		Path:  "/",
	}
	http.SetCookie(c.Writer, cookie)
	//用户号Cookie
	cookie = &http.Cookie{
		Name:  "ID",
		Value: strconv.Itoa(user1.UserNo),
		Path:  "/",
	}
	http.SetCookie(c.Writer, cookie)
	c.JSON(http.StatusOK, gin.H{
		"code":     "loginOK",
		"userRank": userRank,
	})
	return
}
